package com.zxs.admin.controller;

import com.zxs.admin.service.AdminProducetService;
import com.zxs.admin.utils.AliyunOSSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import parm.PageParam;
import parm.ProductSaveParam;
import pojo.Product;
import utils.R;

import java.util.UUID;

/**
 * @author zxs
 * *9 19:25
 */
@RestController
@RequestMapping("product")
public class ProductSearchController {

    @Autowired
    private AdminProducetService adminProducetService;


    @Autowired
    private AliyunOSSUtils aliyunOssUtils;

    @GetMapping("list")
    public R list(PageParam pageParam){
        return  adminProducetService.listPage(pageParam);
    }


    @PostMapping("upload")
    public R upload( @RequestParam("img") MultipartFile img) throws Exception {
        //uuid 文件名就多了
        String filename = img.getOriginalFilename();
        filename = UUID.randomUUID().toString().replace("-","")+filename;

        String contentType = img.getContentType();

        byte[] content = img.getBytes();

        int hours = 1000;
        String url = aliyunOssUtils.uploadImage(filename, content, contentType, hours);
        System.out.println("url = " + url);

        return R.ok("图片上传成功！",url);

    }


    @PostMapping("update")
    public R update(Product product){

        return adminProducetService.update(product);
    }

    @PostMapping("save")
    public R adminSave( ProductSaveParam productSaveParam){

        return adminProducetService.adminSave(productSaveParam);
    }

    @PostMapping("remove")
    R remove( Integer productId){
        return adminProducetService.remove(productId);
    }



}
